0.1 Load the packages

suppressPackageStartupMessages({
library(knitr)
library(pander)
library(dygraphs)
library(zoo)
library(Quandl)
library(ggplot2)
library(rbokeh)
library(networkD3)
})

0.2 R Markdown

In R, there are couple of ways to visualize dataset. The first method is using the R-built in method, like plot, hist functions. But the graphs are not aethetically good. The other immutable images are done commonly using ggplot2 library. Sometimes, we need interactive plots, that’s where D3 based javascript libraries come in handy. The overview will mostly focus on ggplot and interactive plots using D3 library.

0.3 D3 Library

D3 is one of the most popular javascript library for creating interactive plots. D3 Github. It can turn almost all the static images into an interactive plots, including: box plots, bubble chart, calendar view, dendrogram, network graph, Treemap, Confusion Matrix, time series plots etc.

We’ll cover some of the most commonly used D3 library in our group: Dygraphs, Plotly, Rbokeh, leaflet.

0.4 Demo plots

0.4.1 dygraph

brent <- Quandl("EIA/PET_RBRTE_D", collapse="monthly", type="ts")
dygraph(brent, main = "Brent Spot Prices") %>% dyRangeSelector() %>% 
  dyAxis("y", label = "US $/Barrel") %>%
  dySeries("V1", label = "Brent ($/Barrel)") %>%
  dyLegend(show = "follow")

0.4.2 GeoSpatial Visualization

library(leaflet)
## 
## Attaching package: 'leaflet'
## The following object is masked from 'package:networkD3':
## 
##     JS
m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=-77.003, lat=38.9, popup="IHS MARKIT")
m  # Print the map

0.4.3 rbokeh

library(rbokeh)
figure() %>%
  ly_points(Sepal.Length, Sepal.Width, data = iris,
    color = Species, glyph = Species,
    hover = list(Sepal.Length, Sepal.Width))

0.4.4 networkViz

library(visNetwork)
nodes <- data.frame(id = 1:6, title = paste("node", 1:6), 
                    shape = c("dot", "square"),
                    size = 10:15, color = c("blue", "red"))
edges <- data.frame(from = 1:5, to = c(5, 4, 6, 3, 3))
visNetwork(nodes, edges) %>%
  visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)

0.4.5 dendroNetwork

hc <- hclust(dist(USArrests), "ave")

dendroNetwork(hc, height = 600)

0.4.6 plotly

library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- ggplot(data = diamonds, aes(x = cut, fill = clarity)) +
            geom_bar(position = "dodge")
ggplotly(p)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
## Warning in plyr::split_indices(scale_id, n): '.Random.seed' is not an
## integer vector but of type 'NULL', so ignored

0.5 ggplot2 to D3

d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- ggplot(data = d, aes(x = carat, y = price)) +
  geom_point(aes( clarity), size = 4) +
  geom_smooth(aes(colour = cut, fill = cut)) + facet_wrap(~ cut)
p
## `geom_smooth()` using method = 'loess'

library(plotly)
gg <- ggplotly(p)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
## `geom_smooth()` using method = 'loess'
gg